package com.data.modules.online.config.service.p020a;

import com.data.modules.online.cgform.extend.ExtendJsonKey;
import com.data.modules.online.cgform.util.CgformUtil;
import com.data.modules.online.cgform.util.OnlFormShowType;
import org.apache.commons.lang.StringUtils;
import com.data.common.util.ConvertUtils;
import com.data.modules.online.config.executor.ColumnMeta;
import com.data.modules.online.config.executor.DataBaseConst;
import com.data.modules.online.config.service.DbTableHandleI;

public class DbTableOracleHandleImpl implements DbTableHandleI {
    @Override
    public String getAddColumnSql(ColumnMeta columnMeta) {
        return " ADD  " + getAddColumnDataType(columnMeta) + "";
    }

    @Override
    public String getReNameFieldName(ColumnMeta columnMeta) {
        return "RENAME COLUMN  " + columnMeta.getOldColumnName() + " TO " + columnMeta.getColumnName() + "";
    }

    @Override
    public String getUpdateColumnSql(ColumnMeta cgformcolumnMeta, ColumnMeta datacolumnMeta) {
        return " MODIFY   " + getModifyColumnDataType(cgformcolumnMeta, datacolumnMeta) + "";
    }

    @Override
    public String getMatchClassTypeByDataType(String dataType, int digits) {
        String str = "";
        if (dataType.equalsIgnoreCase("varchar2")) {
            str = DataBaseConst.f474i;
        }
        if (dataType.equalsIgnoreCase("nvarchar2")) {
            return DataBaseConst.f474i;
        }
        if (dataType.equalsIgnoreCase("double")) {
            return "double";
        }
        if (dataType.equalsIgnoreCase(DataBaseConst.f473h) && digits == 0) {
            return "int";
        }
        if (dataType.equalsIgnoreCase(DataBaseConst.f473h) && digits != 0) {
            return "double";
        }
        if (dataType.equalsIgnoreCase("int")) {
            return "int";
        }
        if (dataType.equalsIgnoreCase("Date") || dataType.equalsIgnoreCase("Datetime")) {
            return OnlFormShowType.f320d;
        }
        if (dataType.equalsIgnoreCase("blob")) {
            return "blob";
        }
        if (dataType.equalsIgnoreCase("clob")) {
            return ExtendJsonKey.f91e;
        }
        return str;
    }

    @Override
    public String dropTableSQL(String tableName) {
        return " DROP TABLE  " + tableName.toLowerCase() + " ";
    }

    @Override
    public String getDropColumnSql(String fieldName) {
        return " DROP COLUMN " + fieldName.toUpperCase() + "";
    }

    private String getAddColumnDataType(ColumnMeta aVar) {
        String str = "";
        if (aVar.getColunmType().equalsIgnoreCase(DataBaseConst.f474i)) {
            str = aVar.getColumnName() + " varchar2(" + aVar.getColumnSize() + ")";
        } else if (aVar.getColunmType().equalsIgnoreCase(OnlFormShowType.f320d)) {
            str = aVar.getColumnName() + " date";
        } else if (aVar.getColunmType().equalsIgnoreCase("int")) {
            str = aVar.getColumnName() + " NUMBER(" + aVar.getColumnSize() + ")";
        } else if (aVar.getColunmType().equalsIgnoreCase("double")) {
            str = aVar.getColumnName() + " NUMBER(" + aVar.getColumnSize() + CgformUtil.f231E + aVar.getDecimalDigits() + ")";
        } else if (aVar.getColunmType().equalsIgnoreCase("bigdecimal")) {
            str = aVar.getColumnName() + " NUMBER(" + aVar.getColumnSize() + CgformUtil.f231E + aVar.getDecimalDigits() + ")";
        } else if (aVar.getColunmType().equalsIgnoreCase(ExtendJsonKey.f91e)) {
            str = aVar.getColumnName() + " CLOB ";
        } else if (aVar.getColunmType().equalsIgnoreCase("blob")) {
            str = aVar.getColumnName() + " BLOB ";
        }
        return (str + (StringUtils.isNotEmpty(aVar.getFieldDefault()) ? " DEFAULT " + aVar.getFieldDefault() : " ")) + ("Y".equals(aVar.getIsNullable()) ? " NULL" : " NOT NULL");
    }

    private String getModifyColumnDataType(ColumnMeta aVar, ColumnMeta aVar2) {
        String str;
        String str2 = "";
        String realDbType = aVar2.getRealDbType();
        if (!aVar2.getIsNullable().equals(aVar.getIsNullable())) {
            str2 = aVar.getIsNullable().equals("Y") ? "NULL" : "NOT NULL";
        }
        if (aVar.getColunmType().equalsIgnoreCase(DataBaseConst.f474i)) {
            if (ConvertUtils.isEmpty(realDbType) || realDbType.toLowerCase().indexOf(DataBaseConst.f477l) < 0) {
                realDbType = "varchar2";
            }
            str = aVar.getColumnName() + " " + realDbType + "(" + aVar.getColumnSize() + ")" + str2;
        } else if (aVar.getColunmType().equalsIgnoreCase(OnlFormShowType.f320d)) {
            str = aVar.getColumnName() + " date " + str2;
        } else if (aVar.getColunmType().equalsIgnoreCase("int")) {
            str = aVar.getColumnName() + " NUMBER(" + aVar.getColumnSize() + ") " + str2;
        } else if (aVar.getColunmType().equalsIgnoreCase("double")) {
            str = aVar.getColumnName() + " NUMBER(" + aVar.getColumnSize() + CgformUtil.f231E + aVar.getDecimalDigits() + ") " + str2;
        } else if (aVar.getColunmType().equalsIgnoreCase("bigdecimal")) {
            str = aVar.getColumnName() + " NUMBER(" + aVar.getColumnSize() + CgformUtil.f231E + aVar.getDecimalDigits() + ") " + str2;
        } else if (aVar.getColunmType().equalsIgnoreCase("blob")) {
            str = aVar.getColumnName() + " BLOB " + str2;
        } else if (aVar.getColunmType().equalsIgnoreCase(ExtendJsonKey.f91e)) {
            str = aVar.getColumnName() + " CLOB " + str2;
        } else {
            str = "";
        }
        return (str + (StringUtils.isNotEmpty(aVar.getFieldDefault()) ? " DEFAULT " + aVar.getFieldDefault() : " ")) + str2;
    }

    @Override
    public String getCommentSql(ColumnMeta columnMeta) {
        return "COMMENT ON COLUMN " + columnMeta.getTableName() + "." + columnMeta.getColumnName() + " IS '" + columnMeta.getComment() + CgformUtil.f229C;
    }

    @Override
    public String getSpecialHandle(ColumnMeta cgformcolumnMeta, ColumnMeta datacolumnMeta) {
        return null;
    }

    @Override
    public String dropIndexs(String indexName, String tableName) {
        return "DROP INDEX " + indexName;
    }

    @Override
    public String countIndex(String indexName, String tableName) {
        return "select count(*) from user_ind_columns where index_name=upper('" + indexName + "')";
    }
}
